package com.talent.web;

import com.talent.page.Page;
import com.talent.pojo.Depart;
import com.talent.pojo.Major;
import com.talent.service.DepartService;
import com.talent.service.MajorService;
import com.talent.vo.MajorDepartVo;
import com.talent.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/manage/major")
public class MajorController extends BaseController {

    @Autowired
    MajorService majorService;

    @Autowired
    DepartService departService;


    @RequestMapping("/manager")
    public Object manager(MajorDepartVo majorDepartVo, ModelAndView mav, Integer currentPage){
        try {
            Page<MajorDepartVo> page = majorService.selectMajorDepartVoPage(majorDepartVo, currentPage == null ? 1 : currentPage);
            mav.addObject("page", page.getResultList());
            initPageParams(mav, page);

            mav.addObject("majorDepartVo", majorDepartVo);
            mav.setViewName("major/manager");
        }catch (Exception e){
            e.printStackTrace();
            mav.setViewName("/error/500");
        }
        return mav;
    }

    /**
     * @Title: addUI
     * @Description: 去添加页面
     * @return
     */
    @RequestMapping(value = "/addUI",method = RequestMethod.GET)
    public Object addUI(ModelAndView mav) {
        List<Depart> departList = departService.selectAllDepart();
        mav.addObject("list", departList);
        mav.setViewName("major/addUI");
        return mav;
    }


    @RequestMapping("add")
    @ResponseBody
    public Result majorAdd(Major major) {
        try {
            majorService.addMajor(major);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }


    /**
     * @Title: addUI
     * @Description: 去修改页面
     * @return
     */
    @RequestMapping(value = "/editUI",method = RequestMethod.GET)
    public ModelAndView editUI(ModelAndView mav,Integer id) {
        try {
            Major major = majorService.selectById(id);
            List<Depart> departList = departService.selectAllDepart();
            mav.addObject("major",major);
            mav.addObject("list",departList);
            mav.setViewName("major/editUI");
        } catch (Exception e) {
            mav.setViewName("/error/500");
            e.printStackTrace();
        }
        return mav;
    }
    
    @RequestMapping("edit")
    @ResponseBody
    public Result edit(Major major) {

        try {
            majorService.editMajor(major);
            return new Result(true);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false);
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("majorDelete")
    @ResponseBody
    public Result delete(Integer id) {
        try {
            majorService.deleteById(id);
            return new Result(true);
        } catch (Exception e) {
            return new Result(false);
        }
    }

    @RequestMapping(value = "validate", method = RequestMethod.POST)
    @ResponseBody
    public Object validate(String name) {
        try {
            int count = majorService.validate(name);
            if(count>0){
                return renderError(name+"已存在");
            }
            return renderSuccess("");
        } catch (Exception e) {
            return renderError("系统错误");
        }

    }


    /**
     * 批量删除
     * @return
     */
    @RequestMapping("majorDeleteIDs")
    @ResponseBody
    public Result deleteIDs(String ids) {
        try {
            majorService.deleteAll(ids.split(","));
            return new Result(true);
        } catch (Exception e) {
            return new Result(false);
        }
    }


    @RequestMapping("majorDetail")
    public ModelAndView Detail(Integer id) {
        ModelAndView mav = new ModelAndView();
        try {
            MajorDepartVo majorDepartVo = majorService.selectMajorDepartById(id);
            mav.addObject("major", majorDepartVo);
            mav.setViewName("major/detail");
        } catch (Exception e) {
            e.printStackTrace();
            mav.setViewName("common/error");
            mav.addObject("error", "有异常！");
        }
        return mav;
    }
}
